package com.gitee.wsl.mathematics.geometry.d2.rect.ext

import com.gitee.wsl.mathematics.coordinate.d2.Coordinate2
import com.gitee.wsl.mathematics.geometry.d2.rect.RectangleShape2d
import com.gitee.wsl.mathematics.number.compareTo

/**
 * Update this Viewport to enclose itself and the specified viewport. If the specified viewport is empty, nothing is
 * done. If this viewport is empty it is set to the specified viewport.
 *
 * @param left   The left edge being unioned with this viewport
 * @param top    The top edge being unioned with this viewport
 * @param right  The right edge being unioned with this viewport
 * @param bottom The bottom edge being unioned with this viewport
 */
fun<N:Number,V: Coordinate2<N, V>,R:RectangleShape2d<N, V,R>> R.union(left: N, top: N, right: N, bottom: N):R {
    if (!isEmpty) {
        var a = this.left
        var b = this.top
        var c = this.right
        var d = this.bottom
        if (this.left < this.right && this.top < this.bottom) {
            if (this.left > left) a = left
            if (this.top > top) b = top
            if (this.right < right) c = right
            if (this.bottom < bottom) d = bottom
        } else {
            a = left
            b = top
            c = right
            d = bottom
        }
        return create(createPoint2(a,b), createPoint2(c,d))
    }
    return this
}

/**
 * Update this Viewport to enclose itself and the specified viewport. If the specified viewport is empty, nothing is
 * done. If this viewport is empty it is set to the specified viewport.
 *
 * @param v The viewport being unioned with this viewport
 */
fun<N:Number,V: Coordinate2<N, V>,R:RectangleShape2d<N, V,R>> R.union(v: RectangleShape2d<N, *,*>) =
    union(v.left, v.top, v.right, v.bottom)
